import React from 'react';
import { FormPanel, Utils, ExtendReader, type FormPanelObject, InputType } from 'lfun-bg-comp';
import { Modal, message } from 'antd';
import { useNavigate } from 'react-router-dom';
import { GENDER } from '@/constant/basicOptions';
import { ID_TYPE } from '@/constant/studentOptions';
import utils, { getPathRoot } from '@/utils';
import { StuNewStudentSrv } from '@/service/student';

const Create: React.FC<{ show: boolean; onChange: Function }> = ({ show, onChange }) => {
  const navigate = useNavigate();
  const list = [
    {
      title: '',
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      span: 24,
      inputs: [
        {
          label: '学生姓名',
          name: 'student_name',
          key: 'student_name',
          required: true,
          textRange: [0, 20],
        },
        {
          label: '身份证件类型',
          name: 'id_type',
          key: 'id_type',
          required: true,
          relationFields: ['id_number'],
          inputType: InputType.select,
          renderOption: ID_TYPE,
        },
        {
          label: '身份证件号码',
          name: 'id_number',
          key: 'id_number',
          inputType: InputType.text,
          relationFields: ['birthday', 'student_gender'],
          textRange: [0, 30],
          required: true,
          ruleType: (formObjectPanel: FormPanelObject) => {
            const id_type = formObjectPanel.getFieldValue('id_type');
            if (id_type === 'resident_id_card') {
              return 'cnPeopleNo';
            }
            return '';
          },
          renderValidate: (formOjbectPanel: FormPanelObject) => {
            const id_type = formOjbectPanel.getFieldValue('id_type');
            if (id_type === 'resident_id_card') {
              return ExtendReader.validateCnPeopleNo(
                formOjbectPanel.getFieldValue('id_number'),
                formOjbectPanel.getFieldValue('birthday'),
                formOjbectPanel.getFieldValue('student_gender')
              );
            }
            return '';
          },
        },
        {
          label: '性别',
          name: 'student_gender',
          key: 'student_gender',
          relationFields: ['id_number'],
          required: true,
          inputType: InputType.select,
          renderOption: GENDER,
          render: (formObjectPanel: FormPanelObject) => {
            const id_type = formObjectPanel.getFieldValue('id_type');
            if (id_type !== 'resident_id_card') {
              return formObjectPanel.getFieldValue('student_gender');
            }
            return ExtendReader.renderGender('id_number', 'student_gender', formObjectPanel);
          },
        },
        {
          label: '出生年月',
          name: 'birthday',
          key: 'birthday',
          required: true,
          relationFields: ['id_number'],
          inputType: InputType.date,
          render: (formObjectPanel: FormPanelObject) => {
            const id_type = formObjectPanel.getFieldValue('id_type');
            if (id_type !== 'resident_id_card') {
              return formObjectPanel.getFieldValue('birthday');
            }
            return ExtendReader.renderBirthday('id_number', 'birthday', formObjectPanel);
          },
        },
        {
          label: '报名号',
          name: 'enrollment_number',
          key: 'enrollment_number',
          textRange: [0, 20],
        },
      ],
    },
  ];

  return (
    <>
      <Modal title="新生登记新增" width="50%" footer={[]} open={show} onCancel={() => onChange(false)}>
        <FormPanel
          btnsAlign="center"
          restBtnText="返回"
          submitBtnText="确定"
          buttonsDisplay="block"
          onReset={() => onChange(false)}
          onFinish={async (formPanelObject: FormPanelObject) => {
            const values = formPanelObject.getFieldsValue();
            const parmas = {
              school_id: Utils.getProps('schoolInfo')?.school_id,
              ...values,
            };
            parmas.birthday = Utils.formatDate(values.birthday, 'YYYY-MM-DD');
            const r = await StuNewStudentSrv.add(parmas);
            if (r.success) {
              onChange(false);
              navigate(`${getPathRoot()}/newstudent/edit/${r.data.student_id}`, { state: { ...r.data } });
              message.success('新生登记新增成功！');
            } else {
              formPanelObject.reVaildation(r);
            }
            return false;
          }}
          template={list}
        />
      </Modal>
    </>
  );
};

export default Create;
